﻿#region References

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Mss.Data;
using Validation = Mss.Common.DomainObjects.Validation;

#endregion

namespace Mss.Pages
{
    [TemplatePart(Name = "PART_LogOnButton", Type = typeof (Button))]
    [TemplatePart(Name = "PART_UserNameBox", Type = typeof (TextBox))]
    [TemplatePart(Name = "PART_PasswordBox", Type = typeof (PasswordBox))]
    public class LogOnPage : Control
    {
        #region Fields

        private Button _logOnButton;
        private PasswordBox _passwordBox;
        private TextBox _userNameBox;

        #endregion

        #region Dependency Properties

        /// <summary>
        /// Using a DependencyProperty as the backing store for CurrentUser.  
        /// This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty CurrentUserProperty =
            DependencyProperty.Register("CurrentUser", typeof (User), typeof (LogOnPage),
                                        new UIPropertyMetadata(null));


        /// <summary>
        /// Using a DependencyProperty as the backing store for LogOnCommand.  
        /// This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty LogOnCommandProperty =
            DependencyProperty.Register("LogOnCommand", typeof (ICommand), typeof (LogOnPage),
                                        new UIPropertyMetadata(null));


        /// <summary>
        /// Using a DependencyProperty as the backing store for ValidationInfo.  
        /// This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty ValidationInfoProperty =
            DependencyProperty.Register("ValidationInfo", typeof(Validation), typeof(LogOnPage), 
                                        new UIPropertyMetadata(null));

        #endregion

        #region Constructor

        static LogOnPage()
        {
            //Apply default style.
            DefaultStyleKeyProperty.OverrideMetadata(typeof (LogOnPage),
                                                     new FrameworkPropertyMetadata(typeof (LogOnPage)));
        }

        #endregion

        #region Properties

        /// <summary>
        /// Provides information about validation.
        /// </summary>
        public Validation ValidationInfo
        {
            get { return (Validation)GetValue(ValidationInfoProperty); }
            set { SetValue(ValidationInfoProperty, value); }
        }

        /// <summary>
        /// Command which will be excuted when LogOn button will be pressed.
        /// </summary>
        public ICommand LogOnCommand
        {
            get { return (ICommand) GetValue(LogOnCommandProperty); }
            set { SetValue(LogOnCommandProperty, value); }
        }

        /// <summary>
        /// Current User Credetials. 
        /// User name and password which are used on the page.
        /// </summary>
        public User CurrentUser
        {
            get { return (User) GetValue(CurrentUserProperty); }
            set { SetValue(CurrentUserProperty, value); }
        }

        #endregion

        #region Methods

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            _logOnButton = GetTemplateChild("PART_LogOnButton") as Button;
            if (_logOnButton == null)
            {
                throw new MissingFieldException("PART_LogOnButton not found.");
            }

            _userNameBox = GetTemplateChild("PART_UserNameBox") as TextBox;
            if (_userNameBox == null)
            {
                throw new MissingFieldException("PART_UserNameBox not found.");
            }

            _passwordBox = GetTemplateChild("PART_PasswordBox") as PasswordBox;
            if (_passwordBox == null)
            {
                throw new MissingFieldException("PART_PasswordBox not found.");
            }

            _logOnButton.Click += onLogOnButtonClick;
        }

        private void onLogOnButtonClick(object sender, RoutedEventArgs e)
        {
            e.Handled = true;
        }

        #endregion
    }
}